package com.example.demo5072.service;

import com.example.demo5072.pojo.User;
import com.example.demo5072.repository.UserRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
//不对运行时异常RuntimeException回滚生效
@Transactional(rollbackFor = RuntimeException.class)
public class UserServiceImpl implements UserService {

    @Resource
    UserRepository userRepository;

    @Override
    public User findById(String id) {
        return userRepository.getOne(Integer.valueOf(id));
    }

    @Override
    @Transactional
    public List<User> findAll() {

        List<User> users = userRepository.findAll();

        return users;
    }

    @Override
    public User save(User user) {
        return userRepository.save(user);
    }

    @Override
    public void delete(String id) {

        userRepository.deleteById(Integer.valueOf(id));
    }
}
